package com.example.wang.myapplication;

import com.google.gson.Gson;
import com.google.gson.internal.$Gson$Types;
import com.liqi.nohttputils.interfa.OnIsRequestListener;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author jiangyun
 * @version 创建时间：2017/9/7 09:17
 */
public abstract class OnMyIsRequestListener<T> implements OnIsRequestListener<String> {

    @Override
    public void onNext(String t) {
        T o = new Gson().fromJson(t, mType);
        onResponse(o);
    }

    public abstract void onResponse(T t);

    Type mType;

    public OnMyIsRequestListener() {
        mType = getSuperclassTypeParameter(getClass());
    }

    static Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
    }

}
